package com.nbjtjc.safe.service.cost;

import cn.hutool.core.util.ObjectUtil;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.nbjtjc.safe.api.cost.$missing$;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostBudgetItemMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetMonthMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetPlanMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetSubItemMapper;
import com.nbjtjc.safe.model.cost.CostBudgetItem;
import com.nbjtjc.safe.model.cost.CostBudgetMonth;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.cost.CostBudgetSubItem;

@Service
public class CostBudgetSubItemService {
	@Autowired
	private CostBudgetSubItemMapper costBudgetSubItemMapper;
	@Autowired
	  private CostBudgetItemMapper costBudgetItemMapper;
	@Autowired
	private  CostBudgetMonthMapper costBudgetMonthMapper;
	@Autowired
	private  CostBudgetPlanMapper costBudgetPlanMapper;
	@Autowired
	private CostBudgetPlanService costBudgetPlanService;
	
	@Transactional(rollbackFor = {Throwable.class})
	public Result deleteMultiple(List<CostBudgetSubItem> costBudgetSubItems){
		Result result=new Result();
		
		if(CollectionUtil.isNotEmpty(costBudgetSubItems)){
			for(CostBudgetSubItem costBudgetSubItem:costBudgetSubItems){
				if(ObjectUtil.isNotNull(costBudgetSubItem)&&ObjectUtil.isNotNull(costBudgetSubItem.getId())){
					result=delete(costBudgetSubItem);
					if(result.isSuccess()==false){
						return result;
					}
				}
			}
		}
		
		return result;
	}
	
	@Transactional(rollbackFor = {Throwable.class})
	public Result delete(CostBudgetSubItem costBudgetSubItem){
	      Result result=new Result();
	      if(ObjectUtil.isNull(costBudgetSubItem.getId())){
	          result.setFalse("作废对象不能为空");
	          return result;
	      }
	      costBudgetSubItem.setCancel(1);
	    //删除子项目记录
	      int res = costBudgetSubItemMapper.updateByPrimaryKeySelective(costBudgetSubItem);
	      if(res==0){
	    	  result.setFalse("作废失败");
		      return result;
	      }
	      //获取子项目完整信息
	      costBudgetSubItem = costBudgetSubItemMapper.selectByPrimaryKey(costBudgetSubItem.getId());
	      
	        res = costBudgetItemMapper.updateCurrentBudget(costBudgetSubItem.getBudgetItemId());
     	 if(res==0){
     		 result.setFalse("更新预算项目当前已分配预算失败");
		         return result;

     	 } 
     	 
     	 	//获取子项目月份记录完整信息
            CostBudgetMonth costBudgetMonthSubItem=new CostBudgetMonth();
            costBudgetMonthSubItem.setItemId(costBudgetSubItem.getId());
            costBudgetMonthSubItem.setItemType(2);
            costBudgetMonthSubItem = costBudgetMonthMapper.selectOne(costBudgetMonthSubItem);
            //删除子项目月份记录
            if(ObjectUtil.isNotNull(costBudgetMonthSubItem)){
                costBudgetMonthMapper.deleteByPrimaryKey(costBudgetMonthSubItem.getId());
            }
            
            //获取大类完整信息
            if(ObjectUtil.isNull(costBudgetSubItem.getBudgetItemId())){
  	    	  result.setFalse("子项目的上级项目为空");
  		      return result;
            }
            CostBudgetItem costBudgetItem = costBudgetItemMapper.selectByPrimaryKey(costBudgetSubItem.getBudgetItemId());
            if(ObjectUtil.isNotNull(costBudgetItem)||ObjectUtil.isNotNull(costBudgetItem.getId())){
            	//获取大类月份记录完整信息
                CostBudgetMonth costBudgetMonthItem = costBudgetMonthMapper.findItemByItemId(costBudgetItem.getId());
                
                
                //计算减去本子项目后的汇总金额和月份金额
                //计算并更新一级项目汇总金额
                minusCostBudgetItem(costBudgetItem,costBudgetSubItem);
                costBudgetItemMapper.updateByPrimaryKeySelective(costBudgetItem);
                //计算并更新一级项目月份记录
                minusCostBudgetMonth(costBudgetMonthItem,costBudgetMonthSubItem);
                costBudgetMonthMapper.updateByPrimaryKeySelective(costBudgetMonthItem);
                
                //获取预算计划完整信息
                CostBudgetPlan costBudgetPlan = costBudgetPlanMapper.selectByPrimaryKey(costBudgetItem.getBudgetPlanId());
                //获取预算计划月份记录完整信息
                if(ObjectUtil.isNotNull(costBudgetPlan)&&ObjectUtil.isNotNull(costBudgetPlan.getId())){
                	CostBudgetMonth costBudgetMonthPlan=costBudgetMonthMapper.findPlanByPlanId(costBudgetPlan.getId());
                	
                    
                    
                    //计算并更新预算计划汇总金额
                	minusCostBudgetPlan(costBudgetPlan,costBudgetSubItem);
                	costBudgetPlanMapper.updateByPrimaryKeySelective(costBudgetPlan);
                    //计算并更新预算计划月份记录
                	 minusCostBudgetMonth(costBudgetMonthPlan,costBudgetMonthSubItem);
                    costBudgetMonthMapper.updateByPrimaryKeySelective(costBudgetMonthPlan);
                }
                

                
            }
            
	      return result;
	  }
	
	//月份数据相减，返回减后的被减月份
	public void minusCostBudgetMonth(CostBudgetMonth costBudgetMonthA,CostBudgetMonth costBudgetMonthB){
		costBudgetMonthA.setJan((ObjectUtil.isNotNull(costBudgetMonthA.getJan())?costBudgetMonthA.getJan():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getJan())?costBudgetMonthB.getJan():0));
		costBudgetMonthA.setFeb((ObjectUtil.isNotNull(costBudgetMonthA.getFeb())?costBudgetMonthA.getFeb():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getFeb())?costBudgetMonthB.getFeb():0));
		costBudgetMonthA.setMar((ObjectUtil.isNotNull(costBudgetMonthA.getMar())?costBudgetMonthA.getMar():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getMar())?costBudgetMonthB.getMar():0));
		costBudgetMonthA.setApr((ObjectUtil.isNotNull(costBudgetMonthA.getApr())?costBudgetMonthA.getApr():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getApr())?costBudgetMonthB.getApr():0));
		costBudgetMonthA.setMay((ObjectUtil.isNotNull(costBudgetMonthA.getMay())?costBudgetMonthA.getMay():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getMay())?costBudgetMonthB.getMay():0));
		costBudgetMonthA.setJun((ObjectUtil.isNotNull(costBudgetMonthA.getJun())?costBudgetMonthA.getJun():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getJun())?costBudgetMonthB.getJun():0));
		costBudgetMonthA.setJul((ObjectUtil.isNotNull(costBudgetMonthA.getJul())?costBudgetMonthA.getJul():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getJul())?costBudgetMonthB.getJul():0));
		costBudgetMonthA.setAug((ObjectUtil.isNotNull(costBudgetMonthA.getAug())?costBudgetMonthA.getAug():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getAug())?costBudgetMonthB.getAug():0));
		costBudgetMonthA.setSep((ObjectUtil.isNotNull(costBudgetMonthA.getSep())?costBudgetMonthA.getSep():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getSep())?costBudgetMonthB.getSep():0));
		costBudgetMonthA.setOct((ObjectUtil.isNotNull(costBudgetMonthA.getOct())?costBudgetMonthA.getOct():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getOct())?costBudgetMonthB.getOct():0));
		costBudgetMonthA.setNov((ObjectUtil.isNotNull(costBudgetMonthA.getNov())?costBudgetMonthA.getNov():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getNov())?costBudgetMonthB.getNov():0));
		costBudgetMonthA.setDece((ObjectUtil.isNotNull(costBudgetMonthA.getDece())?costBudgetMonthA.getDece():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getDece())?costBudgetMonthB.getDece():0));
		costBudgetMonthA.setTotalBudget((ObjectUtil.isNotNull(costBudgetMonthA.getTotalBudget())?costBudgetMonthA.getTotalBudget():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getTotalBudget())?costBudgetMonthB.getTotalBudget():0));
		costBudgetMonthA.setTotalPlanBudget((ObjectUtil.isNotNull(costBudgetMonthA.getTotalPlanBudget())?costBudgetMonthA.getTotalPlanBudget():0)-(ObjectUtil.isNotNull(costBudgetMonthB.getTotalPlanBudget())?costBudgetMonthB.getTotalPlanBudget():0));
		
//		return costBudgetMonthA;
	}
	
	//大类汇总金额减子项目金额
	public void minusCostBudgetItem(CostBudgetItem costBudgetItem,CostBudgetSubItem costBudgetSubItem){
		costBudgetItem.setItemCurrentBudget((ObjectUtil.isNotNull(costBudgetItem.getItemCurrentBudget())?costBudgetItem.getItemCurrentBudget():0)
				-(ObjectUtil.isNotNull(costBudgetSubItem.getItemBudget())?costBudgetSubItem.getItemBudget():0));
		costBudgetItem.setItemTotalBudget((ObjectUtil.isNotNull(costBudgetItem.getItemTotalBudget())?costBudgetItem.getItemTotalBudget():0) 
				-(ObjectUtil.isNotNull(costBudgetSubItem.getItemBudget())?costBudgetSubItem.getItemBudget():0) );
		costBudgetItem.setAccountSum((ObjectUtil.isNotNull(costBudgetItem.getAccountSum())?costBudgetItem.getAccountSum():0)
				-(ObjectUtil.isNotNull(costBudgetSubItem.getAccountSum())?costBudgetSubItem.getAccountSum():0) );
		costBudgetItem.setAccountRemain((ObjectUtil.isNotNull(costBudgetItem.getItemTotalBudget())?costBudgetItem.getItemTotalBudget():0)
				-(ObjectUtil.isNotNull(costBudgetItem.getAccountSum())?costBudgetItem.getAccountSum():0));
		
//		return costBudgetItem;
	}
	
	//预算计划汇总金额减子项目金额
	public void minusCostBudgetPlan(CostBudgetPlan costBudgetPlan,CostBudgetSubItem costBudgetSubItem){
		costBudgetPlan.setTotalBudget((ObjectUtil.isNotNull(costBudgetPlan.getTotalBudget())?costBudgetPlan.getTotalBudget():0)
				-(ObjectUtil.isNotNull(costBudgetSubItem.getItemBudget())?costBudgetSubItem.getItemBudget():0));
		
//		return costBudgetPlan;
	}
	
	//根据大类查子项目
	public Result findByItemId(CostBudgetItem costBudgetItem){
	      Result result=new Result();
	      
	      if(ObjectUtil.isNotNull(costBudgetItem)&&ObjectUtil.isNotNull(costBudgetItem.getId())){
	    	  
	    	  List<CostBudgetSubItem> costBudgetSubItems =costBudgetSubItemMapper.findByItemId(costBudgetItem.getId());
	    	  
	    	  result.setData(costBudgetSubItems);
	      }else{
	    	  result.setFalse("传入的大类ID为空");
	    	  return result;
	      }
	      
	      
	      return result;
	}

		
	@Transactional(rollbackFor = {Throwable.class})
	public Result add(CostBudgetSubItem costBudgetSubItem){
	      Result result=new Result();
	      if(ObjectUtil.isNull(costBudgetSubItem.getBudgetItemId())){
	          result.setFalse("关联上级预算项目ID不能为空");
	          return result;
	      }
	      
	      //无id为新建
	      if(ObjectUtil.isNull(costBudgetSubItem.getId())){
		      costBudgetSubItem.setCancel(0);
		      int res = costBudgetSubItemMapper.insertSelective(costBudgetSubItem);
		      if(res==0){
		    	  result.setFalse("添加预算子项目失败");
			      return result;
		      }
		      //新建月份记录 
		      CostBudgetItem costBudgetItem = costBudgetItemMapper.selectByPrimaryKey(costBudgetSubItem.getBudgetItemId());
		      
		      CostBudgetMonth costBudgetMonth=new CostBudgetMonth();
		      costBudgetMonth.setItemId(costBudgetSubItem.getId());
		      costBudgetMonth.setItemType(2);
		      if(ObjectUtil.isNotNull(costBudgetItem.getBudgetPlanId())){
		    	  costBudgetMonth.setPlanId(costBudgetItem.getBudgetPlanId());;
		    	  
			      CostBudgetPlan costBudgetPlan = costBudgetPlanMapper.selectByPrimaryKey(costBudgetItem.getBudgetPlanId());

			      if(ObjectUtil.isNotNull(costBudgetPlan.getYear())){
		        	  costBudgetMonth.setYear(costBudgetPlan.getYear());
			      }
		      }
		      //新建月份
		      costBudgetMonthMapper.insertSelective(costBudgetMonth);
		      
		      
	      }else{//有id为更新
		      costBudgetSubItem.setCancel(0);
		      int res = costBudgetSubItemMapper.updateByPrimaryKeySelective(costBudgetSubItem);
		      if(res==0){
		    	  result.setFalse("更新预算子项目失败");
			      return result;
		      }
	      }
	      
	        int res = costBudgetItemMapper.updateCurrentBudget(costBudgetSubItem.getBudgetItemId());
       	 if(res==0){
       		 result.setFalse("更新预算项目当前已分配预算失败");
		         return result;

       	 } 
	      
	      result.setData(costBudgetSubItem);
	      return result;
	  }
	
	
}
